<?php
class post extends Api {

    public function index() {

        foreach ( $_POST as $key => $val ) {
            if ( $key == 'alias' ) $val = str_replace( " ", "-", strtolower( $val ) );
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        $rows[ 'dateline' ] = time();
        $rows[ 'isTop' ] = isset( $rows[ 'isTop' ] ) ? $rows[ 'isTop' ] : 0;
        $rows[ 'isHide' ] = isset( $rows[ 'Hide' ] ) ? $rows[ 'Hide' ] : 0;
        $rows[ 'isLock' ] = isset( $rows[ 'isLock' ] ) ? $rows[ 'isLock' ] : 0;

        // 添加文章
        $this->model->table = "article";
        if ( $this->model->find( array( 'title' => $rows[ 'title' ] ) ) ) {
            echo json_encode( array( 'code' => '001', 'msg' => '文章已存在' ) );
            exit;
        }
        $id = $this->model->create( $rows );

        // 对文章分类计数 +1
        $this->model->table = "sort";
        $this->model->incrField( array( 'id' => $rows[ 'sid' ] ), 'total' );

        // 添加文章标签
        $this->model->addTag( $id );

        echo json_encode( array( 'code' => '000', 'msg' => '文章发布成功' ) );
    }
}